﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WR.BL.BE;
using WR.BL.BCC;

namespace WR.UI.MobWebApp
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //string strUname = Page.Request.QueryString["u"];//uname
            //string strPswd = Page.Request.QueryString["p"];//password
            //string strMand = Page.Request.QueryString["m"];//mandante
            //string strLangu = Page.Request.QueryString["l"];//langu
            //string strInst = Page.Request.QueryString["i"];//instancia
            //string strServer = Page.Request.QueryString["s"];//server
            //string strSysId = Page.Request.QueryString["sid"];//sys id
            //string strSapRouter = Page.Request.QueryString["sr"];//sap router
            
            //string retval = string.Empty;

            //if (string.IsNullOrEmpty(strUname) || string.IsNullOrEmpty(strPswd) || string.IsNullOrEmpty(strMand))
            //    //string.IsNullOrEmpty(strInst) || string.IsNullOrEmpty(strServer) || string.IsNullOrEmpty(strSysId))
            //{
            //    retval = "Operación inválida.";
            //}
            //else
            //{
            //    try
            //    {
            //        //-- Formar cadena de conexión:
            //        RfcConfigParametersBE objConn = new RfcConfigParametersBE();
            //        objConn.Id = Session.SessionID;
            //        objConn.Langu = strLangu;
            //        objConn.Instance = strInst;
            //        objConn.Mandt = strMand;
            //        objConn.Passwd = strPswd;
            //        objConn.SapRouter = strSapRouter;
            //        objConn.Server = strServer;
            //        objConn.SysId = strSysId;
            //        objConn.Uname = strUname;

            //        retval = HelperBC.getConnectionId(objConn);
            //        if (string.IsNullOrEmpty(retval))
            //        {
            //            retval = "ID: " + objConn.Id;
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        retval = ex.Message;
            //    }
            //    retval = retval.IndexOf("TIMEDOUT") >= 0 ? "No se pudo establecer la conexión con el servidor." : retval;
            //}
            //Page.Response.Write(retval);
            lblErrorMsg.InnerHtml = string.Empty;
            if (!Page.IsPostBack)
            {
                string retVal = HelperBC.logOut(Session.SessionID);
                if (!string.IsNullOrEmpty(retVal)) { lblErrorMsg.InnerHtml = retVal; }
                Session.RemoveAll();
                Session.Clear();
                Session.Abandon();
                Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
            }
        }

        protected void btnLogin1_Click(object s, EventArgs e)
        {
            string strUname = txtUname.Value.Trim();
            string strPswd = txtPasswd.Value.Trim();
            string strTienda = txtTienda.Value.Trim().ToUpper();

            string retval = string.Empty;

            if (string.IsNullOrEmpty(strUname) || string.IsNullOrEmpty(strPswd)
                || string.IsNullOrEmpty(strTienda))
            {
                retval = "Debe ingresar todos los campos requeridos.";
            }
            else
            {
                try
                {
                    //-- Formar cadena de conexión:
                    RfcConfigParametersBE objConn = new RfcConfigParametersBE();
                    objConn.Id = Session.SessionID;
                    objConn.Passwd = strPswd;
                    objConn.Uname = strUname;
                    objConn.tienda = strTienda;

                    retval = HelperBC.getConnectionId(objConn);
                }
                catch (Exception ex)
                {
                    retval = ex.Message;
                }
                retval = retval.IndexOf("TIMEDOUT") >= 0 ? "No se pudo establecer la conexión con el servidor." : retval;
            }
            lblErrorMsg.InnerHtml = retval;
            if (!string.IsNullOrEmpty(retval))
            {
                Session.RemoveAll();
                Session.Clear();
                Session.Abandon();
                Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
            }
            else
            {
                //logeado con éxito:
                UserBE objUsr = new UserBE();
                string valtda = string.Empty;
                int vTienda = -1;
                objUsr.Usuario = strUname;
                objUsr.Tienda = strTienda;
                RfcConfigParametersBE objConn = new RfcConfigParametersBE();
                valtda = HelperBC.validaTienda(strTienda, Session.SessionID, ref vTienda);
                if (!string.IsNullOrEmpty(valtda))
                {
                    if (vTienda == 0)
                    {
                        Session["UserBE"] = objUsr;
                        Response.Redirect("main.aspx");
                    }
                    else
                        lblErrorMsg.InnerHtml = "No tiene autorización para la tienda que ingresó";
                }
                else
                    lblErrorMsg.InnerHtml = "Ingrese tienda válida";
            }
        }
    }
}
